using System;
using System.Text.RegularExpressions;
using Gtk;
using System.Diagnostics;


namespace JiveMessengerLinux
{

	[System.ComponentModel.ToolboxItem(true)]
	public partial class Login : Gtk.Bin
	{
		public static bool IsUserConnecting = false;
		public static bool IsRetrying = false;
		public static bool StopRetrying = false;
		public event EventHandler<LoginEventArgs> LoginEvent;

		public Login ()
		{
			this.Build ();

			lblStatusText.Text="";
            this.LoginEntry.Text = StaticSettings.Server??"";
            this.PasswordEntry.Text = StaticSettings.Password??"";
            this.UsernameEntry.Text = StaticSettings.Username??"";
            this.checkbuttonAutoLogin.Active = StaticSettings.AutoLogin;
            this.checkbuttonRememberMe.Active = StaticSettings.RememberMe;
            this.checkbuttonRememberPassword.Active = StaticSettings.RememberPassword;

            this.LoginEntry.Changed +=  HandleServerChanged;
            this.UsernameEntry.Changed += HandleUsernameChanged;
            this.PasswordEntry.Changed += HandlePasswordChanged;

            this.checkbuttonAutoLogin.Clicked += HandleCheckbuttonAutoLogin_Clicked;
            this.checkbuttonRememberMe.Clicked += HandleCheckbuttonRememberMe_Clicked;
            this.checkbuttonRememberPassword.Clicked += HandleCheckbuttonRememberPassword_Clicked;
//			this.bnRegister = new Gtk.LinkButton("Register");
//			this.bnResetPassword = new Gtk.LinkButton("Forgot Password?");
		}

        private void HandleCheckbuttonRememberPassword_Clicked(object sender, EventArgs e)
        {
            CheckButton chk = sender as CheckButton;
            StaticSettings.RememberPassword = chk.Active;
        }

        private void HandleCheckbuttonRememberMe_Clicked(object sender, EventArgs e)
        {            CheckButton chk = sender as CheckButton;
            StaticSettings.RememberMe = chk.Active;
        }

       private void HandleCheckbuttonAutoLogin_Clicked (object sender, EventArgs e)
        {
            CheckButton chk = sender as CheckButton;
            StaticSettings.AutoLogin = chk.Active;
        }

      private void HandlePasswordChanged (object sender, EventArgs e)
        {
            Gtk.Entry entry = sender as Gtk.Entry;
            StaticSettings.Password = StaticMethods.EncodePassword(entry.Text);
        }

       private void HandleUsernameChanged(object sender, EventArgs e)
        {
            Gtk.Entry entry = sender as Gtk.Entry;
            StaticSettings.Username = entry.Text;
        }

      private void HandleServerChanged (object sender, EventArgs e)
        {
            Gtk.Entry entry = sender as Gtk.Entry;
            StaticSettings.Server = entry.Text;
        }

		public void SetStatusText(string text)
		{
			lblStatusText.Text= text;
		}
		public void DisableLoginButton()
		{
			bnLogin.Sensitive = false;
		}
		public void EnableLoginButton()
		{
			bnLogin.Sensitive = true;
		}

		protected void OnBnLoginClicked (object sender, EventArgs e)
		{

            this.bnLogin.Sensitive = false;
            string username = StaticSettings.Username;

            Match m = Regex.Match(username, @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
            if (m.Success)
            {
                IsUserConnecting = true;
                this.LoginEvent(this, new LoginEventArgs(username, StaticSettings.Password));
                //pictureBoxLogin.Image = Properties.Resources.jive_small;
            }
            else
            {
                this.SetStatusText("Your EMail Address is invalid");
                this.bnLogin.Sensitive = true;
            }

			//LoginEvent(null,new LoginEventArgs(StaticSettings.Username,StaticSettings.Password));
		}

        protected void OnBnRegisterClicked(object sender, EventArgs e)
        {
            try
            {
                Uri u =new Uri(StaticSettings.Server.ToLower().StartsWith("http://")? StaticSettings.Server: "Http://" + StaticSettings.Server);
                Process.Start(u.AbsoluteUri);
            }
            catch (Exception err)
            {
                MessageBox.Show(String.Format("Error opening url. \n {0})", err.Message));
            }
        }

        protected void OnBnResetPasswordClicked(object sender, EventArgs e)
        {
            try
            {

                string baseServer = StaticSettings.Server.ToLower().StartsWith("http://") ? StaticSettings.Server : "Http://" + StaticSettings.Server;
                Uri u = new Uri(baseServer+"/resetpassword.php");
                Process.Start(u.AbsoluteUri);
            }
            catch (Exception err)
            {
                MessageBox.Show(String.Format("Error opening url. \n {0})", err.Message));
            }         
        }

		public class LoginEventArgs : EventArgs
		{
			public LoginEventArgs(string username, string password)
			{
				this.Username = username;
				this.Password = password;
			}
			
			public string Username { get; set; }
			public string Password { get; set; }
		}
	}
}

